<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="108px">
      <el-form-item label="年级" prop="year">
        <el-select v-model="queryParams.year" clearable filterable placeholder="请选择年级" @change="getGradechange">
          <el-option
            v-for="dict in dict.type.year_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classCode" clearable filterable placeholder="请选择姓名"
        @change="getStuchange">
          <el-option
            v-for="item in classList"
            :key="item.id"
            :label="item.label"
            :value="parseInt(item.value)"
            size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="stuId">
        <el-select v-model="queryParams.stuId" clearable filterable placeholder="请选择姓名">
          <el-option
            v-for="item in namelist"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-circle-close"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" icon="el-icon-download" size="mini" @click="handleExport" >导出</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="年级" align="center"  prop="year" :formatter="gradeTypeFormat" />
      <el-table-column label="班级" align="center"  prop="classTitle"/>
      <el-table-column label="姓名" align="center"  prop="name"/>
      <el-table-column label="患病时间" align="center"  prop="illnessDate"/>
      <el-table-column label="是否传染" align="center"  prop="ifInfect" :formatter="isCurrentFormat1"/>
      <el-table-column label="是否治愈" align="center"  prop="cureStatus" :formatter="isCurrentFormat"/>
      <el-table-column label="病情描述" align="center" prop="remark"   :show-overflow-tooltip="true"/>
      <el-table-column label="疾病处理方式" align="center"  prop="method"   :formatter="illnessProcessFormat"/>
      <el-table-column label="治疗建议" align="center" prop="treatAdvise"   :show-overflow-tooltip="true"> </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="年级" prop="year">
        <el-select v-model="form.year" clearable filterable placeholder="请选择年级"
        :disabled="title=='修改学生患病情况'?true:false"
        @change="getGradechange">
          <el-option
            v-for="dict in dict.type.year_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
        <el-form-item label="班级" prop="classCode">
          <el-select v-model="form.classCode" clearable filterable
          @change="getStuchange"   :disabled="title=='修改学生患病情况'?true:false"
          placeholder="请选择班级">
            <el-option
              v-for="item in classList"
              :key="item.value"
              :label="item.label"
              :value="parseInt(item.value)"
              size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="stuId">
          <el-select v-model="form.stuId" clearable filterable
          :disabled="title=='修改学生患病情况'?true:false" placeholder="请选择姓名">
            <el-option
              v-for="item in namelist"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="患病时间" prop="illnessDate">
          <el-date-picker clearable size="small"
                          v-model="form.illnessDate"
                          type="date"
                          value-format="yyyy-MM-dd "
                          placeholder="选择患病时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否传染" prop="ifInfect">
                    <el-radio-group v-model="form.ifInfect">
                      <el-radio
                        v-for="dict in isCurrentOptions"
                        :key="dict.dictValue"
                        :label="dict.dictValue"
                      >{{dict.dictLabel}}</el-radio>
                    </el-radio-group>
                  </el-form-item>
        <el-form-item label="是否治愈" prop="cureStatus">
          <el-radio-group v-model="form.cureStatus">
            <el-radio
              v-for="dict in isCurrentOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="疾病处理方式" prop="method">
          <el-select v-model="form.method" clearable filterable placeholder="请选择疾病处理方式">
            <el-option
              v-for="dict in illnessProcessOptions"
              :key="dict.dictValue"
             :label="dict.dictLabel"
             :value="dict.dictValue"
              size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="病情描述" prop="remark">
          <el-input
            type="textarea"
            v-model="form.remark"
            placeholder="请输入病情描述"
            size="small"
          />
        </el-form-item>
        <el-form-item label="治疗建议" prop="treatAdvise">
          <el-input
            v-model="form.treatAdvise"
            placeholder="如：取10ml糖浆，倒入饮水杯中，加入50ml的温开水冲服即可。"
            type="textarea"
            size="small"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>


import {selectName} from '@/api/base/device'
import {selectClazz} from "@/api/base/clazz";
import {illnessList,addInsert,updateInsert,removeInsert,exportIllnessList} from "@/api/base/illmanager";

export default {
  name: "feedapply",
  dicts:['year_type',],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolCode:this.$store.getters.schoolCode,
        year:'',
        classCode:'',
      },
      dateTime:'',
      classList: [],
      total: 0,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      form: {},
      namelist:[],
      // 表单校验
      rules: {
        year: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        stuId: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        classCode: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        illnessDate: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        ifInfect: [
          { required: true, message: "请选择是否传染", trigger: "blur" }
        ],
        cureStatus: [
          { required: true, message: "请选择是否治愈", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "请输入病情描述", trigger: "blur" }
        ],
        method: [
          { required: true, message: "请选择疾病处理方式", trigger: "blur" }
        ],
        treatAdvise: [
          { required: true, message: "请输入治疗建议", trigger: "blur" }
        ],
      },
      isCurrentOptions:[],
      illnessProcessOptions:[],
      gradeOptions:[],
    };
  },
  created() {
    this.getDicts("year_type").then(response => {
          this.gradeOptions = response.data;
        });
    this.getDicts("yes_or_no").then(response => {
         this.isCurrentOptions = response.data;
       });
     //疾病处理方式字典
     this.getDicts("illness_process_mode").then(response => {
       this.illnessProcessOptions = response.data;
     });
    //用户名
    this.getNamelist()
    //班级
    this.getQueryClazzList();

    this.getList()
  },
  methods: {
    // 导出
    handleExport() {
      this.handleQuery();
      exportIllnessList(this.queryParams).then(response => {
        this.download(response.msg);
      })
    },
    /** 查询班级列表 */
    async getQueryClazzList() {
      try{
        const obj ={
                 schoolCode:this.queryParams.schoolCode,
                 year:this.queryParams.year||this.form.year
               }
        const {rows}=await selectClazz(obj);
        this.classList=rows.map(item=>{
          return {
            value:item.id,
            label:item.title,
          }
        })
      }catch (e) {
        console.log(e)
      }
    },
    /**查询用户列表*/
    async getNamelist(){
      try {
        const obj ={
                 schoolCode:this.queryParams.schoolCode,
                 year:this.queryParams.year||this.form.year,
                 classId:this.queryParams.classCode||this.form.classCode
               }
        const data=await selectName(obj)
        this.namelist=data.rows.map(item=>{
          return{
            label:item.name,
            value:item.id,
          }
        })
      } catch (e){
        console.log(e)
      }
    },
    async getList() {
      try {
        this.loading = true;
        const data=await illnessList(this.queryParams);
        this.studentList = data.rows;
        this.total = data.total;
        this.loading = false;
      } catch (e) {
        console.log(e)
      }
    },
    getGradechange(){
        this.getQueryClazzList();
        this.getNamelist();
        },
    getStuchange(){
      this.getNamelist();
    },
    // 是否治愈当前字典翻译
    isCurrentFormat(row, column) {
      return this.selectDictLabel(this.isCurrentOptions, row.cureStatus);
    },
     // 年级字典翻译
     gradeTypeFormat(row, column) {
            return this.selectDictLabel(this.gradeOptions, row.year);
         },
     // 是否传染当前字典翻译
     isCurrentFormat1(row, column) {
      return this.selectDictLabel(this.isCurrentOptions, row.ifInfect);
    },
     // 疾病处理方式当前字典翻译
     illnessProcessFormat(row, column) {
      return this.selectDictLabel(this.illnessProcessOptions, row.method);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInsert(this.form).then(response => {
              if (response.code == 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addInsert(this.form).then(response => {
              if (response.code == 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该条数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return removeInsert(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      this.reset();
      const id = row.id || this.ids
      this.form = Object.assign({}, this.form, row)
      this.open = true;
      this.title = "修改学生患病情况";
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生患病情况";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      console.log(this.ids);
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams={
        year:'',
        classCode:'',
        stuId:'',
        pageNum: 1,
        pageSize: 10,
      }
      this.getGradechange()
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {
        year:'',
        classCode:'',
        stuId:'',
        illnessDate:'',
        ifInfect:'',
      };
      this.resetForm("form");
    },
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog .el-form-item__content {
  width: 67%;
}
</style>
